class Dictionary {
    constructor() {
        this.items = {};
    }

    // has
    has(value){
        return value in this.items;
    }

    // set
    set(key,value){
        this.items[key] = value;
    }

    // remove
    remove(key){
        if(this.has(key)){
            Reflect.deleteProperty(this.items,key);
        }
    }

    // get
    get(key){
        return this.has(key)?this.items[key]:undefined;
    }

    // values
    values(){
        let values = [];
        for (const key in this.items) {
            if (this.has(key)) {
                values.push(this.items[key]);
            }
        }
        return values;
    }

    // clear
    clear() {
        this.items = {};
    }

    // size
    size() {
        return Reflect.ownKeys(this.items).length;
    }

    // keys
    keys() {
        return Reflect.ownKeys(this.items);
    }

    // getItems
    getItems(){
        return items;
    }
}

class Graph{
    constructor(){
        this.vertices = [];
        this.adjList = new Dictionary();
    }

    // 添加顶点
    addVertex(v){
        this.vertices.push(v);
        this.adjList.set(v,[]);
    }

    // 创建边，连接顶点
    addEdge(v,w){
        this.adjList.get(v).push(w);
        this.adjList.get(w).push(v);
    }

    // 控制台输出
    toString(){
        let str = '';

        for (let i = 0; i < this.vertices.length; i++) {
            str+=`${this.vertices[i]}=>`;

            let neighbors = this.adjList.get(this.vertices[i])
            for (let j = 0; j < neighbors.length; j++) {
                str+=`${neighbors[j]} `
            }
            str += '\n'
        }

        return str;
    }
}
